home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / GRAPHICS.SWG / 0014_Drawing Graphic Circles.pas < prev    next >
Pascal/Delphi Source File  |  1993-08-27  |  3KB  |  102 lines

  1. {
  2. MICHAEL NICOLAI
  3.  
  4.  
  5. The basic formula (and quickest) For drawing a circle is: x^2 + y^2 = r^2.
  6. The r stands For radius (half the diameter). You know this formula, i am
  7. sure. A guy called Phytagoras set i up very long ago to calculate the
  8. hypotenuse of a given triangle. (there has to be a 90° angel between a and b)
  9.  
  10.  
  11.                    |\
  12.                    | \
  13.                  a |  \ c      c^2 = a^2 + b^2
  14.                    |   \
  15.                    |____\
  16.  
  17.                      b
  18.  
  19. Remember?
  20.  
  21. Now look at this:        ...|     a quater of the circle
  22.                        ..   |
  23.                       . ____|y
  24.                      . |\   |
  25.                     .  | \  |
  26.                     .  | r\ |
  27.                     .  |   \|
  28.                --------------------------
  29.                     r  x    |0
  30.                             |
  31.                             |
  32.  
  33.  
  34. r is given and take 0 - r as a starting point For x. Then all you have to do
  35. is to calculate y and plot the point.
  36.  
  37.     y = sqrt((r * r) - (x * x))      sqrt : square root
  38.  
  39. After each calculation x is increased Until it has reached 0. Then one
  40. quarter of the circle is drawn. The other three quarters are symmetrical.
  41.  
  42. I have written a short Program For you to draw a circle in 320x200x256
  43. Graphics mode. When you key in some values please remember that NO error
  44. checking will be done. x has to be between 0 and 319, and y between 0 and
  45. 199. The radius must not be greater than x and y.
  46.  
  47. Example: x : 160; y : 100; r : 90
  48.  
  49. When you start this Program you will not get correct circles because in
  50. Graphics mode ONE pixel is not square!!! You have to calculate an aspect
  51. ratio to get nice looking circles.
  52. }
  53.  
  54. Program circle;
  55.  
  56. Uses
  57.   Crt, Dos;
  58.  
  59. Var
  60.   regs    : Registers;
  61.   x0, y0  : Word;
  62.   x, y, R : Real;
  63.   temp    : Real;
  64.   c       : Char;
  65.  
  66. Procedure putpixel(x, y : Word; color : Byte);
  67. begin
  68.   mem[$A000: (y * 320 + x)] := color;
  69. end;
  70.  
  71. begin
  72.   ClrScr;
  73.   Writeln('Enter coordinates of middle-point :');
  74.   Writeln;
  75.   Write('x : '); readln(x0);
  76.   Write('y : '); readln(y0);
  77.   Writeln;
  78.   Write('Enter radius :'); readln(R);
  79.  
  80.   { Switch to 320x200x256 }
  81.  
  82.   regs.ax := $0013;
  83.   intr($10, regs);
  84.  
  85.   x := (-1) * R;  { go from 0 - R to 0 }
  86.   temp := R * R;
  87.   Repeat
  88.     y := sqrt(temp - (x * x));
  89.     putpixel((x0 + trunc(x)), (y0 - trunc(y)), 15); { 4.th quadrant }
  90.     putpixel((x0 - trunc(x)), (y0 - trunc(y)), 15); { 1.st quadrant }
  91.     putpixel((x0 + trunc(x)), (y0 + trunc(y)), 15); { 3.rd quadrant }
  92.     putpixel((x0 - trunc(x)), (y0 + trunc(y)), 15); { 2.nd quadrant }
  93.     x := x + 0.1; { change this if you want coarse or fine circle. }
  94.   Until (x >= 0.0);
  95.   c := ReadKey;  { wait For keypress. }
  96.  
  97.   { Switch back to Textmode. }
  98.  
  99.   regs.ax := $0003;
  100.   intr($10, regs);
  101. end.
  102.